package com.slx.k_diff_pairs_in_an_array;

import java.util.Arrays;

public class Solution {
    public int findPairs(int[] nums, int k) {
        if (nums == null || nums.length < 2 || k < 0) {
            return 0;
        }

        int result = 0;

        Arrays.sort(nums);

        for (int i = 0; i < nums.length - 1;i++) {

            for (int j = i + 1; j < nums.length && nums[j] - nums[i] <= k; j++) {
                while (j + 1 < nums.length && nums[j] == nums[j + 1]) {
                    j++;
                }

                if (nums[j] - nums[i] == k) {
                    result++;
                }
            }

            while (i + 1 < nums.length && nums[i] == nums[i + 1]) {
                i++;
            }

        }

        return result;
    }
}
